package com.formiaomiao.demain.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.formiaomiao.demain.R;
import com.formiaomiao.demain.adapter.SelectPoiAdapter;
import com.formiaomiao.demain.base.BaseActivity;
import com.formiaomiao.demain.model.SelectPoiBean;
import com.formiaomiao.demain.service.LocationService;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SelectPoiActivity extends BaseActivity implements PoiSearch.OnPoiSearchListener, SelectPoiAdapter.onSelectCheckListener {

    @BindView(R.id.iv_class_back)
    ImageView ivClassBack;
    @BindView(R.id.tv_class_name)
    TextView tvClassName;
    @BindView(R.id.tv_class_submit)
    TextView tvClassSubmit;
    @BindView(R.id.rl_select_poi)
    RecyclerView rlSelectPoi;

    PoiSearch.Query query;
    PoiSearch poiSearch;

    List<SelectPoiBean> list = new ArrayList<>();
    SelectPoiAdapter poiAdapter;

    String address = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_poi);
        ButterKnife.bind(this);
        ViewInit();
    }

    public void ViewInit() {
        tvClassName.setText("选择显示地点");
        tvClassSubmit.setText("确定");
        tvClassSubmit.setVisibility(View.VISIBLE);
        query = new PoiSearch.Query("", "", LocationService.city);
        query.setPageSize(30);
        poiSearch = new PoiSearch(this, query);
        poiSearch.setOnPoiSearchListener(this);
        poiSearch.setBound(new PoiSearch.SearchBound(new LatLonPoint(LocationService.lat,
                LocationService.lng), 1000));//设置周边搜索的中心点以及半径
        poiSearch.searchPOIAsyn();
        poiAdapter = new SelectPoiAdapter(this, list);
        rlSelectPoi.setLayoutManager(new LinearLayoutManager(this));
        rlSelectPoi.setAdapter(poiAdapter);
        poiAdapter.setOnSelectCheckListener(this);
    }


    @Override
    public void onPoiSearched(PoiResult poiResult, int i) {
        for (int j = 0; j < poiResult.getPois().size(); j++) {
            SelectPoiBean bean = new SelectPoiBean();
            bean.setTitle(poiResult.getPois().get(j).getTitle());
            bean.setIsselect(false);
            list.add(bean);
        }
        poiAdapter.notifyDataSetChanged();
    }

    @Override
    public void onPoiItemSearched(PoiItem poiItem, int i) {

    }

    @Override
    public void selectchecklistener(View v, int index) {
        address = list.get(index).getTitle();
        for (int i = 0; i < list.size(); i++) {
            list.get(i).setIsselect(false);
        }
        list.get(index).setIsselect(true);
        poiAdapter.notifyDataSetChanged();
    }

    @OnClick({R.id.iv_class_back, R.id.tv_class_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_class_back:
                finish();
                break;
            case R.id.tv_class_submit:
                Intent intent = new Intent();
                intent.putExtra("address", address);
                setResult(10,intent);
                finish();
                break;
        }
    }
}
